using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace ZSW.Framework.EditorTools
{
    /// <summary>
    /// 修改选中所有预制体的位置、旋转、尺寸
    /// </summary>
    public class RandomTransformByName : EditorWindow
    {
        [SerializeField] private List<Transform> selectedObjects = new List<Transform>();

        [SerializeField] private Vector3 positionDirection = Vector3.zero;
        [SerializeField] private Vector2 randomPositionRange = Vector2.zero;

        [SerializeField] private Vector3 rotationRange = Vector3.zero;
        [SerializeField] private Vector2 randomRotationRange = Vector2.zero;

        [SerializeField] private Vector3 scaleDirection = Vector3.zero;
        [SerializeField] private Vector2 randomScaleRange = Vector2.zero;
        private string prefabName;
        private void OnGUI()
        {
            GUILayout.Label("Settings:", EditorStyles.boldLabel);

            positionDirection = EditorGUILayout.Vector3Field("Position Direction", positionDirection);
            randomPositionRange = EditorGUILayout.Vector2Field("Random Position Range", randomPositionRange);
            rotationRange = EditorGUILayout.Vector3Field("Rotation Range", rotationRange);
            randomRotationRange = EditorGUILayout.Vector2Field("Random Rotation Range", randomRotationRange);
            scaleDirection = EditorGUILayout.Vector3Field("Scale Direction", scaleDirection);
            randomScaleRange = EditorGUILayout.Vector2Field("Random Scale Range", randomScaleRange);

            prefabName = EditorGUILayout.TextField("Prefab Name", prefabName);

            SerializedObject serializedObject = new SerializedObject(this);
            SerializedProperty property = serializedObject.FindProperty("selectedObjects");
            EditorGUILayout.PropertyField(property, true);

            if (GUILayout.Button("Find"))
            {
                FindSameNamePrefab();
            }

            if (GUILayout.Button("Execute"))
            {
                ExecuteTransformChanges();
            }

            if (GUILayout.Button("Reset"))
            {
                Reset();
            }
        }

        private void FindSameNamePrefab()
        {
            selectedObjects.Clear();
            GameObject[] allObjects = GameObject.FindObjectsOfType<GameObject>();

            foreach (GameObject obj in allObjects)
            {
                if (obj.name == prefabName)
                {
                    selectedObjects.Add(obj.transform);
                }
                else if (obj.name.Contains(prefabName))
                {
                    selectedObjects.Add(obj.transform);
                }
            }
        }

        private void ExecuteTransformChanges()
        {
            foreach (Transform obj in selectedObjects)
            {
                Undo.RecordObject(obj, "Transform Change");

                // 生成随机位置变化
                float positionRandomValue = Random.Range(randomPositionRange.x, randomPositionRange.y);
                Vector3 randomPosition = new Vector3(positionDirection.x * positionRandomValue,
                                                     positionDirection.y * positionRandomValue,
                                                     positionDirection.z * positionRandomValue);
                obj.position += randomPosition;

                // 生成随机旋转变化
                float rotationRandomValue = Random.Range(randomRotationRange.x, randomRotationRange.y);
                Quaternion randomRotation = Quaternion.Euler(
                    rotationRange.x * rotationRandomValue,
                    rotationRange.y * rotationRandomValue,
                    rotationRange.z * rotationRandomValue);
                obj.rotation *= randomRotation;

                // 生成随机缩放变化
                float scaleRandomValue = Random.Range(randomScaleRange.x, randomScaleRange.y);
                Vector3 randomScale = new Vector3(scaleDirection.x * scaleRandomValue,
                                                  scaleDirection.y * scaleRandomValue,
                                                  scaleDirection.z * scaleRandomValue);
                obj.localScale += randomScale;
            }
        }

        private void Reset()
        {
            selectedObjects.Clear();
            positionDirection = Vector3.zero;
            randomPositionRange = Vector2.zero;
            rotationRange = Vector3.zero;
            randomRotationRange = Vector2.zero;
            scaleDirection = Vector3.zero;
            randomScaleRange = Vector2.zero;
            prefabName = "";
        }
    }
}